package com.whz.streamdemo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;

import static java.nio.file.Files.lines;
import static java.util.stream.Collectors.*;

/**
 * @Author: sinosoft
 * @Description:
 * @Date: Create in 9:26 2021/5/18
 * @Modified By:
 */
public class Anagrams2 {
    public static void main(String[] args) throws IOException {
        Path dictionary = Paths.get("D:\\IDEACode\\And-then-there-were-none.txt");
        int minGroupSize = Integer.parseInt("20");
        try(Stream<String> words = lines(dictionary)) {
            words.collect(
                    groupingBy(Anagrams2::alphabetize))
                 .values()
                 .stream()
                 .filter(group -> group.size() >= minGroupSize)
                 .forEach(g -> System.out.println(g.size() + ": " + g));
        }
    }
    /*将字符串中的字符按照顺序进行排列*/
    private static String alphabetize(String s) {
        char[] a = s.toCharArray();
        Arrays.sort(a);
        return new String(a);
    }
}
